package com.songlanyun.modules.donationpointsconfig.controller.admin;

import cn.hutool.core.collection.CollectionUtil;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.donationpointsconfig.entity.DonationPointsConfigEntity;
import com.songlanyun.modules.donationpointsconfig.service.DonationPointsConfigService;
import com.songlanyun.modules.exception.ConfigException;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 公益积分捐赠设置
 *
 * @author wuchao
 * @email songlanyun@gmail.com
 * @date 2022-08-02 17:22:40
 */
@RestController
@RequestMapping("/admin/donation/points/config")
public class DonationPointsConfigAdminController extends AbstractController {
    @Autowired
    private DonationPointsConfigService donationPointsConfigService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions(":donationconfig:list")
    public R list() {
        return R.ok().put("list", donationPointsConfigService.list());
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions(":donationconfig:save")
    public R save(@RequestBody List<DonationPointsConfigEntity> list) {
        if (CollectionUtil.isEmpty(list)) {
            return R.ok();
        }
        if (list.size() > 6) {
            throw new RRException(ConfigException.UP_TO_6_SETS_OF_DONATION_RULES_CAN_BE_SET);
        }
        donationPointsConfigService.saveList(list);

        return R.ok();
    }

}
